package handler

import (
	"context"
	"fmt"
	"server/global"
	"server/models"
	__ "server/proto"
)

type Server struct {
	__.UnimplementedVideoServer
}

// SayHello implements helloworld.GreeterServer
func (s *Server) Add(_ context.Context, in *__.AddRequest) (*__.AddResponse, error) {
	add := models.Videos{
		Tittle:  in.Tittle,
		Address: in.Address,
		Name:    in.Name,
		Ms:      in.Ms,
		Music:   in.Music,
	}
	global.DB.Create(&add)
	return &__.AddResponse{
		Id: int64(add.Id),
	}, nil
}
func (s *Server) Update(_ context.Context, in *__.UpdateRequest) (*__.UpdateResponse, error) {
	var video models.Videos
	global.DB.Where("id = ?", in.Id).Find(&video)
	if video.Id == 0 {
		return nil, fmt.Errorf("作品不存在 不能修改")
	}
	add := models.Videos{
		Id:      int32(in.Id),
		Tittle:  in.Tittle,
		Address: in.Address,
		Name:    in.Name,
		Ms:      in.Ms,
		Music:   in.Music,
	}
	global.DB.Create(&add)
	return &__.UpdateResponse{
		Id: int64(add.Id),
	}, nil
}
func (s *Server) List(_ context.Context, in *__.ListRequest) (*__.ListResponse, error) {
	var find []models.Videos
	global.DB.Find(&find)
	var finds []*__.List
	for _, v := range find {
		list := __.List{
			Tittle:  v.Tittle,
			Address: v.Address,
			Name:    v.Name,
			Ms:      v.Ms,
			Music:   v.Music,
		}
		finds = append(finds, &list)
	}
	return &__.ListResponse{
		List: finds,
	}, nil
}
